package com.example.port.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import com.example.port.service.InvoiceService;

import jakarta.annotation.PostConstruct;

import java.util.List;

/**
 * 发票服务代理类，用于解决循环依赖问题
 * 这个类作为StorageFeeServiceImpl和InvoiceServiceImpl之间的桥梁
 */
@Component
public class InvoiceServiceProxy {
    
    private InvoiceService invoiceService;
    
    private final ApplicationContext applicationContext;
    
    @Autowired
    public InvoiceServiceProxy(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }
    
    @PostConstruct
    public void init() {
        // 在所有Bean初始化完成后，从ApplicationContext中获取InvoiceService实例
        this.invoiceService = applicationContext.getBean(InvoiceService.class);
    }
    
    /**
     * 根据仓储费用生成发票
     */
    public int generateInvoiceFromStorageFees(List<Long> storageFeeIds, Integer clientId) {
        return invoiceService.generateInvoiceFromStorageFees(storageFeeIds, clientId);
    }
    
    /**
     * 删除发票
     */
    public int deleteInvoice(Long invoiceId) {
        return invoiceService.deleteInvoice(invoiceId);
    }
    
    /**
     * 解除费用与发票的关联
     */
    public int unlinkFeeFromInvoice(Long feeId) {
        return invoiceService.unlinkFeeFromInvoice(feeId);
    }
} 